package com.wgts.wgts.mvp.view.activity.guessing;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.dialog.JoinPeoplesDialog;
import com.wgts.wgts.control.eventbus.OrderFragmentEvenBus;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerGuessingAndAwardDetailsActivityComponent;
import com.wgts.wgts.dagger.module.GuessingAndAwardDetailsActivityModule;
import com.wgts.wgts.localbean.JoinPeopleData;
import com.wgts.wgts.mvp.presenter.IGuessingAndAwardDetailsActivityPresenter;
import com.wgts.wgts.mvp.view.IGuessingAndAwardDetailsActivityView;
import com.wgts.wgts.netmodle.resp.AwardWinnerDetail;
import com.wgts.wgts.tools.MoHuGlide;
import com.wgts.wgts.tools.Tools;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/*
 *
 *
 *   data:2018/10/1620:19
 *
 */public class GuessingAndAwardDetailsActivity extends BaseActivity implements IGuessingAndAwardDetailsActivityView {

    @Inject
    IGuessingAndAwardDetailsActivityPresenter presenter;
    @BindView(R.id.back_btn_image)
    ImageView backBtnImage;
    @BindView(R.id.back_btn_ly)
    LinearLayout backBtnLy;
    @BindView(R.id.include_title)
    TextView includeTitle;
    @BindView(R.id.include_text)
    TextView includeText;
    @BindView(R.id.include_right_image)
    ImageView includeRightImage;
    @BindView(R.id.include_right_ly)
    LinearLayout includeRightLy;
    @BindView(R.id.order_status)
    TextView orderStatus;
    @BindView(R.id.logistics_company_name)
    TextView logisticsCompanyName;
    @BindView(R.id.logistics_number)
    TextView logisticsNumber;
    @BindView(R.id.logistics_copy)
    TextView logisticsCopy;
    @BindView(R.id.order_logistics_title_ly)
    LinearLayout orderLogisticsTitleLy;
    @BindView(R.id.order_head_ly)
    LinearLayout orderHeadLy;
    @BindView(R.id.logistics_recycle)
    RecyclerView logisticsRecycle;
    @BindView(R.id.include_logistics_Ly)
    LinearLayout includeLogisticsLy;
    @BindView(R.id.affirm_location_image)
    ImageView affirmLocationImage;
    @BindView(R.id.affirm_user_name)
    TextView affirmUserName;
    @BindView(R.id.affirm_user_phone)
    TextView affirmUserPhone;
    @BindView(R.id.affirm_user_location)
    TextView affirmUserLocation;
    @BindView(R.id.include_location_ly)
    LinearLayout includeLocationLy;
    @BindView(R.id.placeholder_title)
    TextView placeholderTitle;
    @BindView(R.id.shopping_car_item_image)
    ImageView shoppingCarItemImage;
    @BindView(R.id.shopping_car_title)
    TextView shoppingCarTitle;
    @BindView(R.id.shopping_car_type_show)
    TextView shoppingCarTypeShow;
    @BindView(R.id.shopping_car_price)
    TextView shoppingCarPrice;
    @BindView(R.id.order_shop_number)
    TextView orderShopNumber;
    @BindView(R.id.shopping_car_details)
    LinearLayout shoppingCarDetails;
    @BindView(R.id.product_ly)
    LinearLayout productLy;
    @BindView(R.id.leave_message)
    TextView leaveMessage;
    @BindView(R.id.img_lottery_user)
    CircleImageView imgLotteryUser;
    @BindView(R.id.tv_lottery_number)
    TextView tvLotteryNumber;
    @BindView(R.id.linear_lottery)
    LinearLayout linearLottery;
    @BindView(R.id.linear_friend_more)
    LinearLayout linearFriendMore;
    @BindView(R.id.circle_friend_img1)
    CircleImageView circleFriendImg1;
    @BindView(R.id.circle_friend_img2)
    CircleImageView circleFriendImg2;
    @BindView(R.id.circle_friend_img3)
    CircleImageView circleFriendImg3;
    @BindView(R.id.circle_friend_img4)
    CircleImageView circleFriendImg4;
    @BindView(R.id.circle_friend_img5)
    CircleImageView circleFriendImg5;
    @BindView(R.id.circle_friend_img6)
    CircleImageView circleFriendImg6;
    @BindView(R.id.linear_join_friend)
    LinearLayout linearJoinFriend;
    @BindView(R.id.order_number)
    TextView orderNumber;
    @BindView(R.id.copy)
    TextView copy;
    @BindView(R.id.include_order_pay_time)
    TextView includeOrderPayTime;
    @BindView(R.id.order_pay_time_ly)
    LinearLayout orderPayTimeLy;
    @BindView(R.id.order_time_ly)
    LinearLayout orderTimeLy;
    @BindView(R.id.submit_button_text)
    TextView submitButtonText;
    @BindView(R.id.submit_button_ly)
    LinearLayout submitButtonLy;
    @BindView(R.id.my_order_parent)
    FrameLayout myOrderParent;

    private int winnId;

    public static final String WINNID = "WINNID";

    private String type;

    public static final String TYPE = "TYPE";

    public static final String GUESS = "GUESS";

    public static final String MORE_MORE = "moreMore";

    //private AwardWinnerDetail awardWinnerDetail;

    private JoinPeoplesDialog joinPeoplesDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.guessing_and_award_details_activity);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    public void init() {
        DaggerGuessingAndAwardDetailsActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .guessingAndAwardDetailsActivityModule(new GuessingAndAwardDetailsActivityModule(this))
                .build()
                .inject(this);

        winnId = Integer.valueOf(getIntent().getStringExtra(WINNID));

        type = getIntent().getStringExtra(TYPE);
    }

    private void initView() {
        presenter.initView(winnId);
    }

    @Override
    public void initViewResult(AwardWinnerDetail awardWinnerDetail) {


        affirmUserName.setText(awardWinnerDetail.userAddress.userName);
        affirmUserPhone.setText(awardWinnerDetail.userAddress.userPhone);
        affirmUserLocation.setText(awardWinnerDetail.userAddress.province + " " + awardWinnerDetail.userAddress.city +
                "" + awardWinnerDetail.userAddress.area + " " + awardWinnerDetail.userAddress.details);

        logisticsCompanyName.setText(awardWinnerDetail.expressType);

        logisticsNumber.setText(awardWinnerDetail.expressCode);


        MoHuGlide.getInstance().glideImg(this, awardWinnerDetail.orderProduct.productSmallImage, shoppingCarItemImage);

        shoppingCarTitle.setText(awardWinnerDetail.orderProduct.productName);

        shoppingCarPrice.setText("￥" + awardWinnerDetail.orderProduct.productPrice);

        orderShopNumber.setText(awardWinnerDetail.orderProduct.drawNumber + "人场次");

        MoHuGlide.getInstance().glideImg(this, awardWinnerDetail.winningUserAvatar, imgLotteryUser);

        tvLotteryNumber.setText(awardWinnerDetail.winningUserNickName);


        if (awardWinnerDetail.participateUser.size() > 5) {
            circleFriendImg6.setVisibility(View.VISIBLE);
        }

        for (int i = 0; i < awardWinnerDetail.participateUser.size(); i++) {
            switch (i) {
                case 0:
                    MoHuGlide.getInstance().glideImg(this, awardWinnerDetail.participateUser.get(i).userImage, circleFriendImg1);
                    circleFriendImg1.setVisibility(View.VISIBLE);
                    break;
                case 1:
                    MoHuGlide.getInstance().glideImg(this, awardWinnerDetail.participateUser.get(i).userImage, circleFriendImg2);
                    circleFriendImg2.setVisibility(View.VISIBLE);
                    break;
                case 2:
                    MoHuGlide.getInstance().glideImg(this, awardWinnerDetail.participateUser.get(i).userImage, circleFriendImg3);
                    circleFriendImg3.setVisibility(View.VISIBLE);
                    break;
                case 3:
                    MoHuGlide.getInstance().glideImg(this, awardWinnerDetail.participateUser.get(i).userImage, circleFriendImg4);
                    circleFriendImg4.setVisibility(View.VISIBLE);
                    break;
                case 4:
                    MoHuGlide.getInstance().glideImg(this, awardWinnerDetail.participateUser.get(i).userImage, circleFriendImg5);
                    circleFriendImg5.setVisibility(View.VISIBLE);
                    break;
            }
        }

        orderNumber.setText(awardWinnerDetail.drawOrderCode);

        includeOrderPayTime.setText(awardWinnerDetail.drawTime);

        switch (awardWinnerDetail.winningState){
            case "1" :
                submitButtonLy.setEnabled(false);

                break;
            case "2" :
                submitButtonLy.setEnabled(false);
                break;
            case "3" :
                submitButtonLy.setEnabled(false);
                break;
            case "4" :
                if(awardWinnerDetail.expressState == 0){
                    submitButtonLy.setEnabled(false);
                    submitButtonText.setText("待发货");
                    orderStatus.setText("待发货");
                }else{
                    submitButtonLy.setEnabled(true);
                    submitButtonText.setText("确认收货");
                    orderStatus.setText("确认收货");
                    submitButtonLy.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            presenter.ensureAwardOrder(winnId);
                        }
                    });
                }
                break;
            case "5" :
                submitButtonLy.setEnabled(true);
                submitButtonText.setText("联系客服");
                submitButtonLy.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //TODO
                        Tools.call(GuessingAndAwardDetailsActivity.this);
                    }
                });
                submitButtonLy.setVisibility(View.GONE);
                break;
            case "6" :
                submitButtonLy.setEnabled(false);
                break;
        }


        ArrayList<JoinPeopleData> joinPeopleDatas = new ArrayList<>();

        for(AwardWinnerDetail.ParticipateUser item : awardWinnerDetail.participateUser){
            JoinPeopleData joinPeopleData = new JoinPeopleData();
            joinPeopleData.name = item.userNickName;
            if(type.equals(GUESS)){
                joinPeopleData.price = String.valueOf(item.winnerNumber);
            }else {
                joinPeopleData.price = String.valueOf(item.winnerNumber);
            }
            switch (item.winningState){
                case "1":
                    joinPeopleData.status = "";
                    break;
                case "2":
                    joinPeopleData.status = "";
                    break;
                case "3":
                    joinPeopleData.status = "已竞中";
                    break;
                case "4":
                    joinPeopleData.status = "";
                    break;
                case "5":
                    joinPeopleData.status = "";
                    break;
                case "6":
                    joinPeopleData.status = "";
                    break;
            }
            joinPeopleData.time = item.winningTime;
            joinPeopleData.userHeadImag = item.userImage;

            joinPeopleDatas.add(joinPeopleData);
        }


        joinPeoplesDialog = JoinPeoplesDialog.getJoinPeoplesDialog(awardWinnerDetail.participateUser.size() + "/" + awardWinnerDetail.participateUser.size(), joinPeopleDatas);



    }

    @Override
    public void ensureSuccess() {
       presenter.initView(winnId);
    }

    @Override
    public void onError(int status, String msg) {
        MoHuToast.showToast(msg);
    }

    @OnClick({R.id.back_btn_ly, R.id.linear_friend_more})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn_ly:
                finish();
                break;
            case R.id.linear_friend_more:
                if(joinPeoplesDialog != null){
                    joinPeoplesDialog.show(getSupportFragmentManager(), "");
                }
                break;
        }
    }

    @Override
    public void finish(){
        super.finish();
        EventBus.getDefault().post(new OrderFragmentEvenBus());
    }
}
